<?php
include_once ("sys_info.php");               //获取板块信息
include_once ("../../config_controller.php");//后台的config，和前台类似。后期要整合成一个

$system_part = addslashes($_REQUEST ['sys_part']);//获取当前所在板块
$theme_folder = host_theme($_SERVER['HTTP_HOST']);//取得进入所输入的域名，确定theme_folder
include_once '../../front/' . $theme_folder . '/connection.php';
include_once '../common/script/check_session.php';

?>




<!-- 读取前台的function，用来获取板块显示隐藏 -->
<!-- 读取前台的config,，用来获取板块显示隐藏  -->
<?php include_once("function.php"); ?>
<?php include '../../front/' . $theme_folder . '/config.php'; ?>





<?

/* 问题:典雅店铺看不到后台更新的产品，需要重复更新
   原因:后台添加产品没有在 relation表 添加关联店铺
 *
 * 另外解决方法：；
 * evolution\front\coresection\list-function.php  210行
 *
 * */

$cat_list_json = json_decode($_REQUEST['cat_list_json'],true);  //关联文章信息处理

$new_cat_list = '';

if($_REQUEST['cat_list_json'] != ''){
	$cat_list_arr = array();
	foreach($cat_list_json as $cat_val){

		if(!empty($cat_val['children'])){
			$cat_list_arr [] = get_deep_id($cat_val['children']);
		}
		else{
			$cat_list_arr [] = $cat_val['id'];
		}

	}

	$new_cat_list = toOneArray($cat_list_arr);

}


$brand_list_json = json_decode($_REQUEST['brand_list_json'],true);  //关联文章信息处理


$new_brand_list = '';

if($_REQUEST['brand_list_json'] != ''){
	$brand_list_arr = array();
	foreach($brand_list_json as $brand_val){

        //父子联动启动的时候写法，只记录子类
//		if(!empty($brand_val['children'])){
//			$brand_list_arr [] = get_deep_id($brand_val['children']);
//		}
//		else{
//			$brand_list_arr [] = $brand_val['id'];
//		}

        //父子联动取消的时候写法，记录父类和子类
        $brand_list_arr [] = get_deep_id($brand_val['children']);
        $brand_list_arr [] = $brand_val['id'];

	}
	static $res_arr = array();
	$new_brand_list = toOneArray($brand_list_arr,true);

}


function toOneArray($array,$clear=false)
{
    static $res_arr = array();
	if ($clear)
    {
        $res_arr = array();
    }
    foreach ($array as $v) {
        if (is_array($v)) {
            toOneArray($v);
        } else {
            $res_arr[] = $v;
        }
    }
    return $res_arr;
}

function get_deep_id($list){
	$id_arr = array();
	foreach($list as $val){
		if(!empty($val['children'])){
			return  get_deep_id($val['children']);
		}

		$id_arr[] = $val['id'];

	}

	return $id_arr;
}

//设置需要搜索的表格的前缀后缀
$table_Suffix = $sys_list_table;
$table = $table_Prefix . $table_Suffix;
//设置需要搜索的表格的前缀后缀


//设置需要搜索的表格的排序项和升降项
$sort_by = "nid";
$sort_order = "desc";
//设置需要搜索的表格的排序项和升降项



$detail_attr_list = json_decode($_REQUEST['detail_attr'],true);  //关联文章内容

$goods_attr_list = json_decode($_REQUEST['goods_attr'],true);  //关联文章信息处理


?>





<?
//设置各图片的默认值,如果没有上传，都会是记录透明的图片
$icon = "alpha.gif";
$banner = "alpha.gif";
$destination1 = "alpha.gif";
$destination2 = "alpha.gif";
$destination3 = "alpha.gif";
$destination4 = "alpha.gif";
$destination5 = "alpha.gif";
$destination6 = "alpha.gif";
$destination7 = "alpha.gif";
$destination8 = "alpha.gif";
$destination9 = "alpha.gif";
$destination10 = "alpha.gif";



?>



<?

//这里是设计上传文件的部分：包含图片和附件。视频也应该用这种形式上传
//统一设置一下保存图片的路径
$_uf['path']        = ROOT_PATH_ . "/uploadfiles/".$theme_folder."/";





//上传icon
if (is_uploaded_file($_FILES['icon']['tmp_name']))
{
$_uf['upfile']       = $_FILES['icon'];
$_info               = upload_file($_uf);
$_REQUEST['ai_icon'] = $_info['name'];

    $icon          = $_REQUEST['ai_icon'];


}

//上传Banner
if (is_uploaded_file($_FILES['banner']['tmp_name']))
{
$_uf['upfile']       = $_FILES['banner'];
$_info               = upload_file($_uf);
$_REQUEST['ai_banner'] = $_info['name'];
    $banner        = $_REQUEST['ai_banner'];

}

//上传image1
if (is_uploaded_file($_FILES['image1']['tmp_name']))
{
$_uf['upfile']         = $_FILES['image1'];
$_info                 = upload_file($_uf);
$_REQUEST['ai_image1'] = $_info['name'];
    $destination1  = $_REQUEST['ai_image1'];


}

//上传image2
if (is_uploaded_file($_FILES['image2']['tmp_name']))
{
$_uf['upfile']         = $_FILES['image2'];
$_info                 = upload_file($_uf);
$_REQUEST['ai_image2'] = $_info['name'];
    $destination2  = $_REQUEST['ai_image2'];


}

//上传image3
if (is_uploaded_file($_FILES['image3']['tmp_name']))
{
$_uf['upfile']         = $_FILES['image3'];
$_info                 = upload_file($_uf);
$_REQUEST['ai_image3'] = $_info['name'];
    $destination3  = $_REQUEST['ai_image3'];


}


//上传image4
if (is_uploaded_file($_FILES['image4']['tmp_name']))
{
$_uf['upfile']         = $_FILES['image4'];
$_info                 = upload_file($_uf);
$_REQUEST['ai_image4'] = $_info['name'];
    $destination4  = $_REQUEST['ai_image4'];


}



//上传image5
if (is_uploaded_file($_FILES['image5']['tmp_name']))
{
$_uf['upfile']         = $_FILES['image5'];
$_info                 = upload_file($_uf);
$_REQUEST['ai_image5'] = $_info['name'];
    $destination5  = $_REQUEST['ai_image5'];

}





//上传image6
if (is_uploaded_file($_FILES['image6']['tmp_name']))
{
$_uf['upfile']         = $_FILES['image6'];
$_info                 = upload_file($_uf);
$_REQUEST['ai_image6'] = $_info['name'];
    $destination6  = $_REQUEST['ai_image6'];

}


//上传image7
if (is_uploaded_file($_FILES['image7']['tmp_name']))
{
$_uf['upfile']         = $_FILES['image7'];
$_info                 = upload_file($_uf);
$_REQUEST['ai_image7'] = $_info['name'];
    $destination7  = $_REQUEST['ai_image7'];

}


//上传image8
if (is_uploaded_file($_FILES['image8']['tmp_name']))
{
$_uf['upfile']         = $_FILES['image8'];
$_info                 = upload_file($_uf);
$_REQUEST['ai_image8'] = $_info['name'];
    $destination8  = $_REQUEST['ai_image8'];

}


//上传image9
if (is_uploaded_file($_FILES['image9']['tmp_name']))
{
$_uf['upfile'] = $_FILES['image9'];
$_info                 = upload_file($_uf);
$_REQUEST['ai_image9'] = $_info['name'];
    $destination9  = $_REQUEST['ai_image9'];

}


//上传image10
if (is_uploaded_file($_FILES['image10']['tmp_name'])) {
$_uf['upfile']          = $_FILES['image10'];
$_info                  = upload_file($_uf);
$_REQUEST['ai_image10'] = $_info['name'];
    $destination10 = $_REQUEST['ai_image10'];
}



//上传备用图片
if (is_uploaded_file($_FILES['extend_img']['tmp_name'])) {
$_uf['upfile']             = $_FILES['extend_img'];
$_info                     = upload_file($_uf);
$_REQUEST['ai_extend_img'] = $_info['name'];

}


//上传附件
if (is_uploaded_file($_FILES['attachment']['tmp_name'])) {
$_uf["allowExt"] = array('jpeg',
'jpg',
'png',
'gif',
'rar',
'zip',
'7zip',
'exe',
'msi',
'txt',
'doc',
'xlsx',
'pptx',
'pdf',
    'webp');
$_uf['upfile']             = $_FILES['attachment'];
$_info                     = upload_file($_uf);
$_REQUEST['ai_attachment'] = $_info['name'];

}

?>


<?
//龙纳项目需要添加的   start ---------------------------
//include '../longna/add-function.php'
// 龙纳项目需要添加的 end ---------------------------------------------------------
?>


<?php








$new_detail_name          = addslashes($_REQUEST['detail_name']);
$new_detail_en_name          = addslashes($_REQUEST['detail_en_name']);
$new_detail_model         = addslashes($_REQUEST['detail_model']);
$new_detail_mid           = addslashes($_REQUEST['detail_mid']);
$new_detail_introduce     = addslashes($_REQUEST['detail_introduce']);
$new_detail_introduce2     = addslashes($_REQUEST['detail_introduce2']);
$new_detail_introduce3     = addslashes($_REQUEST['detail_introduce3']);



$new_detail_title         = addslashes($_REQUEST['detail_title']);
$new_detail_keyword       = addslashes($_REQUEST['detail_keyword']);
$new_detail_description   = addslashes($_REQUEST['detail_description']);


//如果没有url会出错，所以强制补充一个
if(!$_REQUEST['detail_url'])
{
    $_REQUEST['detail_url']=date("YmdHis");
}

//上一个页面例用js实现转拼音，所以这行代码可以去掉
//$new_detail_name_str      = str_replace(" ", "-", $new_detail_name);
$new_detail_url      = $_REQUEST['detail_url'];

$new_detail_detail        = addslashes($_REQUEST['detail_detail']);
$new_detail_detail2        = addslashes($_REQUEST['detail_detail2']);
$new_detail_detail3        = addslashes($_REQUEST['detail_detail3']);




$new_detail_stock         = addslashes($_REQUEST['detail_stock']);

if(!$_REQUEST['detail_price']){$_REQUEST['detail_price']=0;}
if(!$_REQUEST['detail_memberprice']){$_REQUEST['detail_memberprice']=0;}
if(!$_REQUEST['detail_discountprice']){$_REQUEST['detail_discountprice']=0;}
if(!$_REQUEST['detail_clickrate']){$_REQUEST['detail_clickrate']=0;}
if(!$_REQUEST['detail_markorder']){$_REQUEST['detail_markorder']=0;}
if(!$_REQUEST['detail_vip']){$_REQUEST['detail_vip']=0;}

$new_detail_price         = addslashes($_REQUEST['detail_price']);
$new_detail_danwei         = addslashes($_REQUEST['ai_danwei']);

$new_detail_memberprice   = addslashes($_REQUEST['detail_memberprice']);
$new_detail_discountprice = addslashes($_REQUEST['detail_discountprice']);
$new_detail_local         = addslashes($_REQUEST['detail_local']);
$new_detail_hotsale       = addslashes($_REQUEST['detail_hotsale']);
$new_detail_discount      = addslashes($_REQUEST['detail_discount']);
$new_detail_recommend     = addslashes($_REQUEST['detail_recommend']);
$new_detail_soldout       = addslashes($_REQUEST['detail_soldout']);
$new_detail_publish       = addslashes($_REQUEST['detail_publish']);

$new_detail_clickrate     = addslashes($_REQUEST['detail_clickrate']);


$new_detail_onsaletime    = addslashes($_REQUEST['detail_onsaletime']);
$new_detail_modifytime    = addslashes($_REQUEST['detail_modifytime']);
$new_detail_inputtime     = date("Y-m-d H:i:s",time());



$new_detail_mark          = addslashes($_REQUEST['detail_mark']);
$new_detail_markorder     = addslashes($_REQUEST['detail_markorder']);
$new_detail_listorder     = addslashes($_REQUEST['detail_listorder']);
$new_termorder            = addslashes($_REQUEST['termorder']);


$new_detail_link          = addslashes($_REQUEST['detail_link']);
$new_detail_storage       = addslashes($_REQUEST['detail_storage']);
$new_detail_vip           = addslashes($_REQUEST['detail_vip']);
$new_detail_short_desc           = addslashes($_REQUEST['detail_short_desc']);

$icon_alt           = addslashes($_REQUEST['ai_icon_alt']);



//检查表是否有这个字段,如果没有自动生成,
//unid是数据指纹。20200223126003+xxxxxxxxxxxx10位随机数组成，是绝对唯一的
$unid           = date("YmdHis").rand(1000000000,9999999999);



//检查表是否有这个字段,如果没有自动生成,
//unid是数据指纹。20200223126003+xxxxxxxxxxxx10位随机数组成，是绝对唯一的


//设置需要搜索的表格的前缀后缀
$table_Suffix = $sys_list_table;
$table = $table_Prefix . $table_Suffix;
//设置需要搜索的表格的前缀后缀

$query_check = mysql_query("select count(nid) from $table where url = '$new_detail_url'");
//检查转换的url是否有重复
$sum_check = mysql_result($query_check, 0);

$term_query_check = mysql_query("select count(nid) from ". $table_Prefix ."term where url = '$new_detail_url'");
//检查转换的url是否有重复
$term_sum_check = mysql_result($term_query_check, 0);

if($sum_check>0 || $term_sum_check >0 ){

    $new_detail_url = $new_detail_url."-D".get_max_nid("detail");
    //url重复的话在后面添加-Dnid
}

$sql = "INSERT INTO $table (
mid,
title,
name,
en_name,
short_desc,
keyword,
description,
url,
model,
introduce,
introduce2,
introduce3,
detail,
detail2,
detail3,
stock,
price,
danwei,
memberprice,
discountprice,
hotsale,
discount,
recommend,
soldout,
local,
clickrate,
modifytime,
publish,
onsaletime,
image1,
image2,
image3,
image4,
image5,
image6,
image7,
image8,
image9,
image10,
icon,
video,
map_address,
mark,
listorder,
markorder,
termorder,
link,
storage,
banner,
attachment,
belong,
vip_term,
extend_img,
inputtime,
video_path,
video_time,
video_img,
video_path2,
video_time2,
video_img2,
unid,
icon_alt
)

value (
'$new_detail_mid',
'$new_detail_title',
'$new_detail_name',
'$new_detail_en_name',
'$new_detail_short_desc',
'$new_detail_keyword',
'$new_detail_description',
'$new_detail_url',
'$new_detail_model',
'$new_detail_introduce',
'$new_detail_introduce2',
'$new_detail_introduce3',
'$new_detail_detail',
'$new_detail_detail2',
'$new_detail_detail3',
'$new_detail_stock',
'$new_detail_price',
'$new_detail_danwei',
'$new_detail_memberprice',
'$new_detail_discountprice',
'$new_detail_hotsale',
'$new_detail_discount',
'$new_detail_recommend',
'$new_detail_soldout',
'$new_detail_local',
'$new_detail_clickrate',
'$new_detail_modifytime',
'$new_detail_publish',
'$new_detail_onsaletime',
'$destination1',
'$destination2',
'$destination3',
'$destination4',
'$destination5',
'$destination6',
'$destination7',
'$destination8',
'$destination9',
'$destination10',
'$icon',
'".addslashes($_REQUEST['detail_video'])."',
'".addslashes($_REQUEST['detail_map_address'])."',
'$new_detail_mark',
'$new_detail_listorder',
'$new_detail_markorder',
'$new_termorder',
'$new_detail_link',
'$new_detail_storage',
'$banner',
'$attachment',
'$sys_belong',
'$new_detail_vip',
'$extend_img',
'$new_detail_inputtime',
'".addslashes($_REQUEST['detail_video_path'])."',
'".addslashes($_REQUEST['detail_video_time'])."',
'".addslashes($_REQUEST['detail_video_img'])."',
'".addslashes($_REQUEST['detail_video_path2'])."',
'".addslashes($_REQUEST['detail_video_time2'])."',
'".addslashes($_REQUEST['detail_video_img2'])."',
'".$unid."',
'".$icon_alt."'
)";

sql_log($sql);
$query = mysql_query($sql) or die (mysql_error()."<br/>".$sql);




//数据插入完毕，现在获取最大值的nid
$sql_max_nid = "select * from $table where nid=(select max(nid) from $table)";

$result = mysql_query($sql_max_nid) or die(mysql_error());

$max_nid = mysql_fetch_array($result);


if(count($detail_attr_list) > 0){


	foreach($detail_attr_list as $val){

		$sql = "INSERT INTO ".$table_Prefix."detail_relation ( `goods_id`, `detail_id`) VALUES ('".$max_nid[0] ."','".$val['value']."')";

		sql_log($sql);
mysql_query($sql);
	}
}


if(count($goods_attr_list) > 0){



    foreach($goods_attr_list as $val){

        $sql = "INSERT INTO ".$table_Prefix."goods_term_relation ( `product_id`, `goods_id`) VALUES ('".$max_nid[0] ."','".$val['value']."')";

        sql_log($sql);
        mysql_query($sql);
    }
}


mysql_close($con);


//设置需要搜索的表格的前缀后缀
$table_Suffix = "relation";
$table = $table_Prefix . $table_Suffix;
//设置需要搜索的表格的前缀后缀


//checkbox传递过来的数组分解开

$term_detail_list = $new_cat_list;

if($term_detail_list ){
	//循环插入detail_nid对应term_nid入数据库
	foreach($term_detail_list as $cat_val){
	//    echo "当前编号" . $i;
	//    echo "<br/>";
	//    echo $term_detail[$i] . "<br/>";

		$sql = "INSERT INTO $table (
	detail_nid,
	term_nid,
	vip_term
	)
	
	value (
	
	'$max_nid[0]',
	'$cat_val',
	'$new_detail_vip'
	)";


		sql_log($sql);
	$query = mysql_query($sql) or die (mysql_error().$sql);

	//echo $sql."<br/><br/>";

	}

}



//checkbox传递过来的数组分解开
$brand_detail_list = $new_brand_list;;


//循环插入detail_nid对应term_nid入数据库
//for ($i = 0; $i < count($brand_detail); $i++) {

if($brand_detail_list ){
	foreach($brand_detail_list as $brand_val){


		$sql = "INSERT INTO $table (
	detail_nid,
	term_nid,
	vip_term,
	belong
	)
	
	value (
	
	'$max_nid[0]',
	'$brand_val',
	'$new_detail_vip',
	'brand'
	)";


		sql_log($sql);
	$query = mysql_query($sql) or die (mysql_error().$sql);

	//echo $sql."<br/><br/>";

	}
}



$img_url_count = count($_FILES['img_url']['tmp_name']);


if($img_url_count > 0){


	foreach($_FILES['img_url']['tmp_name'] as $imgkey=>$imgval){


	if (is_uploaded_file($_FILES['img_url']['tmp_name'][$imgkey])) {
		$upfile = $_FILES["img_url"];
    //获取数组里面的值


		$img_name = explode(".",$upfile["name"][$imgkey]); //上传的文件名改为时间命名
		$name = date("YmdHis",time()).rand(10,99).'.'.$img_name[1];

		$type = $upfile["type"][$imgkey]; //上传文件的类型


		$size = $upfile["size"][$imgkey]; //上传文件的大小
		$tmp_name = $upfile["tmp_name"][$imgkey]; //上传文件的临时存放路径
		/*
		这是一种非常简单文件上传方式。基于安全方面的考虑，您应当增加有关什么用户有权上传文件的限制。
		上传限制
		在这个脚本中，我们增加了对文件上传的限制。用户只能上传 .gif 或 .jpeg 文件
		*/

		switch ($type) {
			case 'image/pjpeg':
				$okType = true;
				break;
			case 'image/jpeg':
				$okType = true;
				break;
			case 'image/gif':
				$okType = true;
				break;
			case 'image/png':
				$okType = true;
				break;
            case 'image/webp':
                $okType = true;
                break;
		}


		if ($okType) {
			/**
			 * 0:文件上传成功<br/>
			 * 1：超过了文件大小，在php.ini文件中设置<br/>
			 * 2：超过了文件的大小MAX_FILE_SIZE选项指定的值<br/>
			 * 3：文件只有部分被上传<br/>
			 * 4：没有文件被上传<br/>
			 * 5：上传文件大小为0
			 */
			$error = $upfile["error"][$imgkey]; //上传后系统返回的值
			echo "================<br/>";
			echo "上传文件名称是：" . $name . "<br/>";
			echo "上传文件类型是：" . $type . "<br/>";
			echo "上传文件大小是：" . $size . "<br/>";
			echo "上传后系统返回的值是：" . $error . "<br/>";
			echo "上传文件的临时存放路径是：" . $tmp_name . "<br/>";

			echo "开始移动上传文件<br/>";
			//把上传的临时文件移动到up目录下面
			move_uploaded_file($tmp_name, '../../uploadfiles/' . $theme_folder . '/' . $name);
			$icon = $name;
			echo "================<br/>";
			echo "上传信息：<br/>";
			if ($error == 0) {
				echo "文件上传成功啦！";
				echo "<br>图片预览:<br>";
				echo "<img src=" . $icon . ">";
				//echo " alt="图片预览:r文件名:".$destination."r上传时间:">";
			} elseif ($error == 1) {
				echo "超过了文件大小，在php.ini文件中设置";
			} elseif ($error == 2) {
				echo "超过了文件的大小MAX_FILE_SIZE选项指定的值";
			} elseif ($error == 3) {
				echo "文件只有部分被上传";
			} elseif ($error == 4) {
				echo "没有文件被上传";
			} else {
				echo "上传文件大小为0";
			}
		} else {
			echo "请上传jpg,gif,png等格式的图片！";
		}

		$img_title = $_REQUEST['img_title'][$imgkey];
		$img_desc = $_REQUEST['img_desc'][$imgkey];
		$img_price = $_REQUEST['img_price'][$imgkey];
		$sql = "INSERT INTO ".$table_Prefix."product_attr ( img_title,img_desc,img_url,img_price , detail_nid) VALUES ('".$img_title ."','".$img_desc."','".$name ."','".$img_price."','".$max_nid[0]."')";

		sql_log($sql);
        $query = mysql_query($sql) or die ("error:".mysql_error());
	}
}

}


?>






<script type="text/javascript">
    alert("输入信息已保存，点击确认返回继续操作.");
    document.location.href = "detail-list.php?sys_part=<? echo $system_part ?>";
</script>
